﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using NPOI.SS.Formula.Functions;
using System.Collections.Generic;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace DFGK_WMS.WebAPI.Controllers.库存管理
{
    /// <summary>
    /// 库存调整
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TInventoryAdjustsController :ControllerBase
    {
        private ITInventoryAdjustsRepository InventoryAdjusts;

        public TInventoryAdjustsController(ITInventoryAdjustsRepository InventoryAdjusts)
        {
            this.InventoryAdjusts = InventoryAdjusts;
        }
        /// <summary>
        /// 库存调整查询
        /// </summary>
        /// <param name="WhId"></param>
        /// <param name="AreaId"></param>
        /// <param name="Status"></param>
        /// <param name="BillSn"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetinventoryAdjusts(int? WhId, int? AreaId, int? Status,
            string? BillSn, int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var list = InventoryAdjusts.GetInventoryAdjusts(out var totalCount, WhId, AreaId, Status, BillSn, pageIndex, pageSize);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));

            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 新建库存信息
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddInventoryAdjusts(TInventoryAdjust item)
        {
            string message;
            var create = InventoryAdjusts.AddInventoryAdjusts(item,out message);
            if (create)
            {
                return Ok(BaseResult.Result(true, "添加成功", "200"));
            }
            return Ok(BaseResult.Result(false, message, "500"));
        }

        /// <summary>
        /// 获取单据号
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string AddDocumentNumber()
        {
            var create = InventoryAdjusts.AddDocumentNumber();

            return create;
        }

        /// <summary>
        /// 根据物料查询信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetWarehouse(int Id)
        {
            if(Id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var list = InventoryAdjusts.GetWarehouse(Id);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200"));

            }
            return Ok(BaseResult.Result(null, "查询失败！", "500"));
        }

        /// <summary>
        /// 更新库存信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateinventoryAdjusts(TInventoryAdjust model)
        {
            if(model.Id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var update = InventoryAdjusts.UpdateInventoryAdjusts(model);

            if (update)
            {
                return Ok(BaseResult.Result(true, "修改成功", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败", "500"));
        }

        /// <summary>
        /// 更新库存信息审核同意
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateReviewConsent(TInventoryApproveDTO model)
        {
            if(model.Id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            List<TUser> get = InventoryAdjusts.GetJobNumberByNowUser(model.NowUserId);

            string[] JobNos = get.Select(p => p.JobNo).ToArray();
            foreach (string JobNo in JobNos)
            {
                if (JobNo != model.JobNO)
                {
                    return Ok(BaseResult.Result(false, "工号错误", "500"));
                }
            }

            var update = InventoryAdjusts.UpdateReviewConsent(model);

            if (update)
            {
                return Ok(BaseResult.Result(true, "修改成功", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败", "500"));

        }

        /// <summary>
        /// 更新库存信息审核驳回
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateReviewRejection(TInventoryApproveDTO model)
        {
            if (model.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            List<TUser> get = InventoryAdjusts.GetJobNumberByNowUser(model.NowUserId);

            string[] JobNos = get.Select(p => p.JobNo).ToArray();
            foreach (string JobNo in JobNos)
            {
                if (JobNo != model.JobNO)
                {
                    return Ok(BaseResult.Result(false, "工号错误", "500"));
                }
            }

            var update = InventoryAdjusts.UpdateReviewRejection(model);

            if (update)
            {
                return Ok(BaseResult.Result(true, "修改成功", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败", "500"));

        }

        /// <summary>
        /// 根据Id获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInventoryAdjustsById(int id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            ApiResult apiResult = new ApiResult();
            var obj = InventoryAdjusts.GetInventoryAdjustsById(id);
            if (obj != null)
            {
                apiResult.code = "200";
                apiResult.message = "获取成功！";
                apiResult.data = obj;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "请输入正确的ID！";
            return Ok(apiResult);
        }

        /// <summary>
        /// 更新审核状态为已调整
        /// </summary>
        /// <param id="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult UpdApproved(int id)
        {
            bool status = InventoryAdjusts.UpdApproved(id);
            if (status)
            {
                return Ok(BaseResult.Result(true, "修改成功", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败", "500"));
        }

    }
}
